<?php

class M_scholarships Extends MY_Model
{
	public function get_scholarships_available_for_stud($id)
	{
		$sql = "SELECT ss_id,
					   ss_name,
					   ss_desc,
					   ss_tuit_percentage,
					   ss_misc_percentage,
					   ss_tuit_cash,
					   ss_misc_cash
				FROM student_scholarships
				WHERE ss_id NOT IN (SELECT scholarship_id 
									FROM student_scholarship_record
								    WHERE stud_e_id = ?
									AND academic_year_id = ?
								   )
			   ";
		$query = $this->db->query($sql,array($id,$this->c_sy->id));
		
		return $query->num_rows() >= 1 ? $query->result() : FALSE;
	}
	

	public function get_selected_scholarship_and_insert_to_deductions($data)
	{
		$sql = "SELECT ss_id,ss_name,ss_desc,ss_rate
				FROM student_scholarships
				WHERE ss_id = ? ";
		$query = $this->db->query($sql,array($data['scholarships']));
		
		if($query->num_rows() >= 1)
		{
			$result = $query->row();
			
			$input['sdr_remarks'] = $data['remarks'];
			$input['sdr_amount'] = $result->ss_rate;
			$input['sdr_deduction_name'] = $result->ss_name;
			$input['sdr_enrollment_id'] = $data['enrollment_id'];
			$input['sdr_scholarship_id'] = $result->ss_id;
			$input['sdr_schoolyear_id'] = $this->c_sy->id;
			$input['sdr_user_trans_id'] = $this->c_user->userid;
			$input['sdr_created'] = NOW;
			
			$this->db->insert('student_deductions_record',$input);
			return $this->db->affected_rows() >=1 ? TRUE : FALSE;
		}else{
			return FALSE;
		}
	}
	
	public function get_student_deductions($id)
	{
		$sql = "SELECT sdr_amount,
					   sdr_remarks,
					   sdr_id
				FROM student_deductions_record
				WHERE sdr_enrollment_id = ?
				AND sdr_schoolyear_id = ?
				";
		$query = $this->db->query($sql,array($id,$this->c_sy->id));
		return $query->num_rows() >=1 ? $query->result() : FALSE;
	}
	
	public function get_student_deduction($id,$sdr)
	{
		$sql = "SELECT sdr_amount,
					   sdr_remarks,
					   sdr_id
				FROM student_deductions_record
				WHERE sdr_enrollment_id = ?
				AND sdr_id = ?
				AND sdr_schoolyear_id = ?
				";
		$query = $this->db->query($sql,array($id,$sdr,$this->c_sy->id));
		return $query->num_rows() >=1 ? $query->row() : FALSE;
	}
	
	public function get_available_deductions($id)
	{
		$mine = '';
		$sql = "SELECT sdr_remarks
				FROM student_deductions_record
				WHERE sdr_enrollment_id = ?
				AND sdr_schoolyear_id = ?
				";
		$query = $this->db->query($sql,array($id,$this->c_sy->id));
		if($query->num_rows() >=1)
		{
			$i = 0;
			foreach($query->result() as $v):
				$i++;
				$mine .= "'".$v->sdr_remarks."'";
				if($i < $query->num_rows()):
					$mine .= ", ";
				endif;
			endforeach;
			
			$q = "SELECT DISTINCT sdr_remarks
					FROM student_deductions_record
					WHERE sdr_enrollment_id != ?
					AND sdr_schoolyear_id = ?
					AND sdr_remarks NOT IN ({$mine})
					UNION
					SELECT label as sdr_remarks
					FROM discount_labels
					";
			$qry = $this->db->query($q,array($id,$this->c_sy->id));
			return $qry->num_rows() >=1 ? $qry->result() : FALSE;
			
		}else{
			
			$q = "SELECT sdr_remarks
					FROM student_deductions_record
					WHERE sdr_enrollment_id != ?
					AND sdr_schoolyear_id = ?
					UNION
					SELECT label as sdr_remarks
					FROM discount_labels
					";
			$qry = $this->db->query($q,array($id,$this->c_sy->id));
			return $qry->num_rows() >=1 ? $qry->result() : FALSE;
			
		}
		
	}
	
	public function get_student_scholarships_deducted($e_id)
	{
		$sql = "SELECT total_value_deducted,scholarship_name,id as sch_id
				FROM student_scholarship_record
				WHERE stud_e_id = ?
				AND academic_year_id = ?";
		$query = $this->db->query($sql,array($e_id,$this->c_sy->id));
		return $query->num_rows() >=1 ? $query->result() : FALSE;
	}
	
	public function sum_all_student_deductions($id)
	{
		$sql = "SELECT sum(sdr_amount) as total_deductions
				FROM student_deductions_record
				WHERE sdr_enrollment_id = ?
				AND sdr_schoolyear_id = ? ";
		$query = $this->db->query($sql,array($id,$this->c_sy->id));
		
		$sql2 = "SELECT sum(total_value_deducted) as total_deductions
				 FROM student_scholarship_record
				 WHERE stud_e_id = ?
				 AND academic_year_id = ?";
		$query2 = $this->db->query($sql2,array($id,$this->c_sy->id));
		
		
		
		$total_deductions = $query->num_rows() >= 1 ? $query->row()->total_deductions : 0 ;
		$total_scholarship = $query2->num_rows() >= 1 ? $query2->row()->total_deductions : 0 ;
		
		return $total_deductions + $total_scholarship;
	}
	
	public function insert_and_process_student_scholarship($enrollment_id,$scholarship_id)
	{	
		//get data for current scholarship entered
		$get_scholarships = $this->db->where('ss_id',$scholarship_id)
									 ->limit(1)
									 ->get('student_scholarships');
		
		//get assigned tuition fee rate to student that
		// is not yet applied with the scholarship
		$get_sef_tuit = $this->db->select('sef_fee_rate,sef_id')
								->where('sef_enrollment_id',$enrollment_id)
								->where('sef_schoolyear_id',$this->c_sy->id)
								->where('is_tuition_fee',1)
								->order_by('sef_created')
								->limit(1)
								->get('student_enrollment_fees');
					
		$TUIT = $get_sef_tuit->num_rows() >= 1 ? $get_sef_tuit->row() : FALSE;
		$SS	  = $get_scholarships->num_rows() >= 1 ? $get_scholarships->row() : FALSE;
		
		if($TUIT AND $SS)
		{	
			// insert selected scholarship to table
			$input['stud_e_id'] = $enrollment_id;
			$input['scholarship_id'] = $scholarship_id;
			$input['academic_year_id'] = $this->c_sy->id;
			$input['tuit_per'] = $SS->ss_tuit_percentage;
			// $input['misc_per'] = $SS->ss_misc_percentage;
			$input['tuit_cash'] = $SS->ss_tuit_cash;
			// $input['misc_cash'] = $SS->ss_misc_cash;
			$input['scholarship_name'] = $SS->ss_name ;
			$input['scholarship_desc'] = $SS->ss_desc;
			$input['original_tuit_rate'] = $TUIT->sef_fee_rate;
			// $input['original_misc_rate'] = $MISC->sef_fee_rate;

			if($SS->ss_tuit_percentage > 0)
			{
				$input['total_value_deducted'] = round((($TUIT->sef_fee_rate / 100) * $SS->ss_tuit_percentage),2);
			}else{
				$input['total_value_deducted'] = $SS->ss_tuit_cash;
			}
			
			$this->db->insert('student_scholarship_record',$input);
			
			
			if($this->db->affected_rows() >= 1)
			{
				//this will minus the added scholarship to the current tuition fee
				
				if($this->_process_scholarship($enrollment_id,$SS,$TUIT))
				{
					$get_scholarships->free_result();
					$get_sef_tuit->free_result();
					return TRUE;
				}else{
					$get_scholarships->free_result();
					$get_sef_tuit->free_result();
					return FALSE;
				}
			}else{
				log_message('error','Scholarship was not inserted to student E-ID. '.$enrollment_id.' with scholarship id '.$scholarship_id);
				return FALSE;
			}
		}else
		{
			log_message('error','Check M_scholarships/insert_and_process_student_scholarship unable to get TUIT,MISC,SS');
			return FALSE;
		}
	}
	
	
	private function _process_scholarship($enrollment_id,$SS,$TUIT)
	{
		
		//set flag to false
		if($enrollment_id AND $SS AND $TUIT)
		{
			// get data from scholarship
			$CI =& get_instance();
		
			//get last payment record id
			$c_spr_id = $CI->M_student_payment_record->current_payment_record_id($enrollment_id);
			$plans = $this->M_fees->get_student_current_payment_mode_and_due_by_enrollment_id($enrollment_id);
			
			$flag = array('tuit_cash'=>FALSE,'tuit_perc'=>FALSE);
			
			/* Update student_enrollment_fees, deduct cash from tuition */
			if(floatval($SS->ss_tuit_cash) !== 0.00)
			{
				$flag['tuit_cash'] = TRUE;
				if($this->_check_applied_schl($enrollment_id,'tuit','cash') == FALSE)
				{
					if($c_spr_id !== NULL)
					{
					
						$breakdwon = $this->db->select('spb_id')->where('spb_spr_id',$c_spr_id)->get('student_payment_breakdown');
						if($breakdwon->num_rows() >= 1 )
						{
							$sql = "UPDATE student_payment_breakdown
									SET spb_fee_rate = spb_fee_rate - {$SS->ss_tuit_cash},
										spb_remaining_balance = spb_remaining_balance - {$SS->ss_tuit_cash}
									WHERE spb_sef_id = ?
									AND spb_stud_enrollment_id = ?
									AND spb_sy_id = ?
									AND spb_is_tuition_fee = 1";
							$this->db->query($sql,array($TUIT->sef_id,$enrollment_id,$this->c_sy->id));
							$update_breakdown = $this->db->affected_rows() >=1 ? TRUE : FALSE;
						}else{
							$update_breakdown = TRUE;
						}
						
						if($update_breakdown)
						{
							$set['sef_fee_rate'] = round($TUIT->sef_fee_rate - $SS->ss_tuit_cash,2);
							$set['tuit_cash_applied'] = 1;
							
							//minus on student payment plan
							/*
							$update_payment_mode = "UPDATE student_payment_modes set spm_value = spm_value - {$SS->ss_tuit_cash}
								WHERE spm_value > {$SS->ss_tuit_cash}
								AND spm_student_id = ?
								AND spm_ay_id = ?
								ORDER BY field(month_name,'January Registration','April','May','June','July','August','September','October','November','December','January','February') DESC
								limit 1";
							$this->db->query($update_payment_mode,array($enrollment_id,$this->c_sy->id));
							*/
							//format payment plan values
							//$updated_payment_plan = $this->_reductor($plans,$SS->ss_tuit_cash);
							//update payment plan values
							//$this->db->update_batch('student_payment_modes',$updated_payment_plan,'spm_id');

							
							$this->db->set($set)
									 ->where('sef_id',$TUIT->sef_id)
									 ->where('is_tuition_fee',1)
									 ->update('student_enrollment_fees');
							
							if($this->db->affected_rows() >= 1)
							{
								$flag['tuit_cash'] = TRUE;
							}else{
								log_message('error','M_scholarships model -> Unable to process tuit_cash');
								$flag['tuit_cash'] = FALSE;
							}
						}else{
							log_message('error','M_scholarships model -> student payment breakdown was not processed successfully');
							$flag['tuit_cash'] = FALSE;
						}
					}else
					{
						$set['sef_fee_rate'] = $TUIT->sef_fee_rate - $SS->ss_tuit_cash;
						$set['tuit_cash_applied'] = 1;
						
						//minus on student payment plan
						/*
						$update_payment_mode = "UPDATE student_payment_modes set spm_value = spm_value - {$SS->ss_tuit_cash}
							WHERE spm_value > {$SS->ss_tuit_cash}
							AND spm_student_id = ?
							AND spm_ay_id = ?
							ORDER BY field(month_name,'January Registration','April','May','June','July','August','September','October','November','December','January','February') DESC
							limit 1";
						$this->db->query($update_payment_mode,array($enrollment_id,$this->c_sy->id));
						*/
						//format payment plan values
						//$updated_payment_plan = $this->_reductor($plans,$SS->ss_tuit_cash);
						//update payment plan values
						//$this->db->update_batch('student_payment_modes',$updated_payment_plan,'spm_id');
						
						$this->db->set($set)
								 ->where('sef_id',$TUIT->sef_id)
								 ->where('is_tuition_fee',1)
								 ->update('student_enrollment_fees');
						
						
						if($this->db->affected_rows() >= 1)
						{
							$flag['tuit_cash'] = TRUE;
						}else{
							log_message('error','M_scholarships model -> Unable to process tuit_cash');
							$flag['tuit_cash'] = FALSE;
						}
					}
				}
			}else
			{
				$flag['tuit_cash'] = TRUE;
			}
			
			/*
			=================END TUIT CASH ===============
			==============================================
			*/
			
			/* Update student_enrollment_fees, deduct percentage from tuition */
			if(intval($SS->ss_tuit_percentage) !== 0)
			{
				$flag['tuit_perc'] = TRUE;
				$eq = round($TUIT->sef_fee_rate - (($TUIT->sef_fee_rate / 100) * $SS->ss_tuit_percentage),2);
				$diff = round((($TUIT->sef_fee_rate / 100) * $SS->ss_tuit_percentage),2);
				
				$amount = $TUIT->sef_fee_rate - $diff;
				// pp($diff);
				// pd($eq);
				if($this->_check_applied_schl($enrollment_id,'tuit') == FALSE)
				{
					
					if($c_spr_id !== NULL)
					{
						
						$breakdwon = $this->db->select('spb_id')->where('spb_spr_id',$c_spr_id)->get('student_payment_breakdown');
						if($breakdwon->num_rows() >= 1 )
						{
							$sql = "UPDATE student_payment_breakdown
								SET spb_fee_rate = spb_fee_rate - {$diff}, 
								    spb_remaining_balance = spb_remaining_balance - {$diff}
								WHERE spb_sef_id = ?
								AND spb_stud_enrollment_id = ?
								AND spb_sy_id = ?
								AND spb_is_tuition_fee = 1
								limit 1";
								
							$this->db->query($sql,array($TUIT->sef_id,$enrollment_id,$this->c_sy->id));
							$update_breakdown = $this->db->affected_rows() >=1 ? TRUE : FALSE;
						}else{
							$update_breakdown = TRUE;
						}

						if($update_breakdown)
						{
							//format payment plan values
							//$updated_payment_plan = $this->_reductor($plans,$diff);
							//update payment plan values
							//$this->db->update_batch('student_payment_modes',$updated_payment_plan,'spm_id');
							
							// $set['sef_fee_rate'] = $eq;
							// $set['tuit_perc_applied'] = 1;
							// pd($set);
							// $this->db->set($set)
									 // ->where('sef_id',$TUIT->sef_id)
									 // ->where('is_tuition_fee',1)
									 // ->update('student_enrollment_fees');
									 
							$sql = "UPDATE student_enrollment_fees
									SET sef_fee_rate = {$amount},
									tuit_perc_applied = 1
									WHERE sef_id = ?
									AND is_tuition_fee = 1
									LIMIT 1";
							$this->db->query($sql,array($TUIT->sef_id));
							
							if($this->db->affected_rows() >= 1)
							{
								$flag['tuit_perc'] = TRUE;
							}else{
								log_message('error','M_scholarships model -> Unable to process tuit_perc');
								$flag['tuit_perc'] = FALSE;
							}
						}else{
							log_message('error','M_scholarships model -> student payment breakdown was not processed successfully');
							$flag['tuit_perc'] = FALSE;
						}
					}else{
						//format payment plan values
						//$updated_payment_plan = $this->_reductor($plans,$diff);
						//update payment plan values
						//$this->db->update_batch('student_payment_modes',$updated_payment_plan,'spm_id');
						
						$sql = "UPDATE student_enrollment_fees
									SET sef_fee_rate = {$amount},
									tuit_perc_applied = 1
									WHERE sef_id = ?
									AND is_tuition_fee = 1
									LIMIT 1";
						$this->db->query($sql,array($TUIT->sef_id));
						if($this->db->affected_rows() >= 1)
						{
							$flag['tuit_perc'] = TRUE;
						}else{
							log_message('error','M_scholarships model -> Unable to process tuit_cash');
							$flag['tuit_perc'] = FALSE;
						}
					}
				}	
			}else{
				$flag['tuit_perc'] = TRUE;
			}
			/*
			=================END TUIT PERC ===============
			==============================================
			*/
			// check if all flags retured true;
			if($flag['tuit_cash'] AND $flag['tuit_perc'])
			{
				return (object)array('status'=>TRUE,'message'=>'Successfully Processed student scholarship');
			}else
			{	
				log_message('error','M_scholarships/_process_scholarship did not return all flags true');
				return (object)array('status'=>FALSE,'message'=>'An Error was encountered, Unable To Process Scholarship');
			}
			

		}else
		{
			show_error('Missing Missing Missing',404);
			log_message('error','M_scholarships/_process_scholarship parameter ID,SS,MISC,TUIT either of them has no data');
			return FALSE;
		}
	}
	
	/*
		function to check if student has already applied the scholarship
		so that we do not reapply the scholarship two times
	*/
	private function _check_applied_schl($enrollment_id,$type ='',$mode = '')
	{
		$sql = 'SELECT ';
		
		if($type == 'tuit')
		{
			$where = 'is_tuition_fee = 1';
			if($mode == 'cash')
			{
				$column =' tuit_cash_applied ';
				$sql .= $column;
			}else{
				$column =' tuit_perc_applied ';
				$sql .= $column;
			}
		}elseif($type == 'misc')
		{
			$where = 'is_misc_fee = 1';
			if($mode == 'cash')
			{
				$column =' misc_cash_applied ';
				$sql .= $column;
			}else{
				$column =' misc_perc_applied ';
				$sql .= $column;
			}
		}
		
		$sql .= ' FROM student_enrollment_fees
				  WHERE sef_enrollment_id = ?
				  AND sef_schoolyear_id = ? 
				  AND '.$where.'
				  LIMIT 1
				  ';
				 
		$query = $this->db->query($sql,array($enrollment_id,$this->c_sy->id));
		
		if($query->num_rows() >= 1)
		{
			switch($column)
			{
				case $column == 'tuit_cash_applied':
					$query->row()->tuit_cash_applied == 1 ? TRUE : FALSE;
				break;
				case $column == 'tuit_perc_applied':
					$query->row()->tuit_perc_applied == 1 ? TRUE : FALSE;
				break;
				case $column == 'misc_cash_applied':
					$query->row()->misc_cash_applied == 1 ? TRUE : FALSE;
				break;
				case $column == 'misc_perc_applied':
					$query->row()->misc_perc_applied == 1 ? TRUE : FALSE;
				break;
			}
		}
	}
	
	public function process_student_deduction($id,$data)
	{
		$data = (object)$data;
		//if($this->_check_payment_mode($id))
		//{
			$get_sef_tuit = $this->db->select('sef_fee_rate,sef_id')
								->where('sef_enrollment_id',$id)
								->where('sef_schoolyear_id',$this->c_sy->id)
								->where('is_tuition_fee',1)
								->order_by('sef_created')
								->limit(1)
								->get('student_enrollment_fees');
							
			$TUIT = $get_sef_tuit->num_rows() >= 1 ? $get_sef_tuit->row() : FALSE;
			
			//check if rate and data passed is not empty
			if($data AND is_numeric($data->deduction_rate) AND $TUIT)
			{
				//load models
				$this->load->model('M_student_payment_record','mspr');
				
				$record_deduction['sdr_amount'] = $data->deduction_rate;
				$record_deduction['sdr_remarks'] = $data->deduction_desc;
				$record_deduction['sdr_enrollment_id'] = $id;
				$record_deduction['sdr_user_trans_id'] = $this->c_user->userid;
				$record_deduction['sdr_schoolyear_id'] = $this->c_sy->id;
				$record_deduction['sdr_created'] = NOW;
				
				$this->db->insert('student_deductions_record',$record_deduction);
				
				if($this->db->affected_rows() >= 1 )
				{
					return $this->_process_student_deduction($id,$data,$TUIT);
				}else{
					return (object)array('stat'=>FALSE,'log'=>'Unable to apply Deduction to student.');
				}
			}else
			{
				log_message('error','Unable to insert deductions to table.');
				return (object)array('stat'=>FALSE,'log'=>'Some files needed by the system was not properly provided. Please try again.');
			}
		//}else
		//{
			//return (object)array('stat'=>FALSE,'log'=>'Student has no payment plan, please set student payment plan before adding deductions.');
		//}

	}
	
	public function _process_student_deduction($id,$data,$TUIT)
	{
		$data = (object)$data;
		
		//get last payment record id
		$this->load->model('M_student_payment_record','mspr');
		$c_spr_id = $this->mspr->current_payment_record_id($id);
	
		//if true, a payment was already made before adding the scholarship
		//so we have to deduct from the tuition as well
		//$plans = $this->M_fees->get_student_current_payment_mode_and_due_by_enrollment_id($id);
		//$updated_payment_plan = $this->_reductor($plans,$data->deduction_rate);
		if($c_spr_id !== NULL)
		{
			
			
			
			$breakdwon = $this->db->select('spb_id')->where('spb_spr_id',$c_spr_id)->get('student_payment_breakdown');
			if($breakdwon->num_rows() >= 1 )
			{
				//update from payment breakdown
				$sql = "UPDATE student_payment_breakdown
						SET spb_fee_rate = spb_fee_rate - {$data->deduction_rate}
						AND spb_remaining_balance = spb_remaining_balance - {$data->deduction_rate}
						WHERE spb_sef_id = ?
						AND spb_stud_enrollment_id = ?
						AND spb_sy_id = ?
						AND spb_is_tuition_fee = 1";

				$this->db->query($sql,array($TUIT->sef_id,$id,$this->c_sy->id));
				$update_breakdown = $this->db->affected_rows() >=1 ? TRUE : FALSE;
			}else{
				$update_breakdown = TRUE;
			}
			
			if($this->db->affected_rows() >=1)
			{
				//minus on student payment plan
				/*
				$update_payment_mode = 
					"UPDATE student_payment_modes set spm_value = spm_value - {$data->deduction_rate}
					WHERE spm_value > {$data->deduction_rate}
					AND spm_student_id = ?
					AND spm_ay_id = ?
					ORDER BY field(month_name,'January Registration','April','May','June','July','August','September','October','November','December','January','February') DESC
					limit 1";
				$this->db->query($update_payment_mode,array($id,$this->c_sy->id));
				*/
				
				//format payment plan values
				//$updated_payment_plan = $this->_reductor($plans,$data->deduction_rate);
				//update payment plan values
				//$this->db->update_batch('student_payment_modes',$updated_payment_plan,'spm_id');
				
				// minus on student enrollment fee		 
				$usef = "UPDATE student_enrollment_fees
						SET sef_fee_rate = sef_fee_rate - {$data->deduction_rate}
						WHERE is_tuition_fee = 1
						AND sef_id = ?";
				$this->db->query($usef,array($TUIT->sef_id));
				
				if($this->db->affected_rows() >= 1)
				{
					return (object)array('stat'=>TRUE,'log'=>'Successfully applied deduction to Student Fee and Payment Plan.');
				}else{
					log_message('error','M_scholarships model -> Unable to process tuit cash deduction');
					return (object)array('stat'=>FALSE,'log'=>'An error was encountered while applying Deductions to Fee and Payment Plan.');
				}
			}else{
				log_message('error','M_scholarships model -> student payment breakdown was not processed successfully');
				return (object)array('stat'=>FALSE,'log'=>'Unable to apply Deduction to payment breakdown,please try again.');
			}
		}else
		{
			// - if spr id is null then no payments where made no deduction will be made on student payment breakdown	
			// - minus on student payment plan
			/*
			$update_payment_mode = 
				"UPDATE student_payment_modes set spm_value = spm_value - {$data->deduction_rate}
				WHERE spm_value > {$data->deduction_rate}
				AND spm_student_id = ?
				AND spm_ay_id = ?
				ORDER BY field(month_name,'January Registration','April','May','June','July','August','September','October','November','December','January','February') DESC
				limit 1";
			$this->db->query($update_payment_mode,array($id,$this->c_sy->id));
			*/
			
			//format payment plan values
			//$updated_payment_plan = $this->_reductor($plans,$data->deduction_rate);
			//update payment plan values
			//$this->db->update_batch('student_payment_modes',$updated_payment_plan,'spm_id');
			
			// minus on student enrollment fee		 
			$usef = "UPDATE student_enrollment_fees
					SET sef_fee_rate = sef_fee_rate - {$data->deduction_rate}
					WHERE is_tuition_fee = 1
					AND sef_id = ?";
			$this->db->query($usef,array($TUIT->sef_id));
			
			if($this->db->affected_rows() >= 1)
			{
				return (object)array('stat'=>TRUE,'log'=>'Successfully applied deduction to Student Fee and Payment Plan.');;
			}else{
				log_message('error','M_scholarships model -> Unable to process tuit cash deduction NO spr');
				return (object)array('stat'=>FALSE,'log'=>'An error was encountered while applying Deductions to Fee and Payment Plan.');;
			}
		}

	}
	
	public function _update_deduction($data,$d_id)
	{
		$this->db->where('sdr_id', $d_id);
		$this->db->update('student_deductions_record', $data); 
		
		if($this->db->affected_rows() >= 1 )
		{	
			return TRUE;
		}else{
			return FALSE;
		}
	}

	public function _delete_deduction($e_id,$d_id)
	{
		$this->db->where('sdr_id',$d_id)->limit(1)->delete('student_deductions_record');
		
		if($this->db->affected_rows() >= 1 )
		{	
			//$this->load->model('M_fees');
			//if($this->M_fees->recompute_student_fees($e_id) AND $this->M_fees->recompute_payment_plan($e_id))
			//{	
			//	$ded = $this->readd_deductions($e_id);
			//	$sch = $this->readd_scholarships($e_id);

			//	if(($sch->stat == 'true' OR $sch->stat == 'noscholar') AND ($ded->stat == 'true' OR $ded->stat =='nodeduct'))
			//	{
					return TRUE;
			//	}else{
			//		return FALSE;
			//	}
			//}else{
			//	return FALSE;
			//}
		}else{
			return FALSE;
		}
	}
	
	public function _check_payment_mode($id)
	{
		$q = $this->db->select('payment_mode')
				 ->where('e_id',$id)
				 ->limit(1)
				 ->get('enrollments');
				 
		if($q->num_rows() >= 1)
		{
			$r = $q->row();
			
			return ($r->payment_mode == 'none' OR $r->payment_mode == NULL) ? FALSE : TRUE;
		}else{
			return FALSE;
		}
	}
}